package org.codeart.api_driver.service;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.codeart.api_driver.remote.ServiceCodeClient;
import org.codeart.api_driver.remote.ServiceDriverUserClient;
import org.codeart.common.constant.DriverCarEnum;
import org.codeart.common.constant.IdentityEnum;
import org.codeart.common.constant.TokenEnum;
import org.codeart.common.response.TokenResponse;
import org.codeart.common.util.JwtUtils;
import org.codeart.common.util.R;
import org.codeart.common.util.RedisPrefixUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class VerificationService {

    @Autowired
    private ServiceDriverUserClient serviceDriverUserClient;

    @Autowired
    private ServiceCodeClient serviceCodeClient;

    @Autowired
    private StringRedisTemplate redisTemplate;

    public boolean checkAndSendVerificationCode(String phone) {
        // 查询该手机号是否存在
        R<Map<Object, Object>> r = serviceDriverUserClient.getUser(phone);
        Map<Object, Object> data = r.getData();
        if (data == null) {
            return false;
        }
        Integer exists = (Integer) data.get("exists");
        if (exists == DriverCarEnum.STATE_INVALID.value()) {
            return false;
        }
        log.info("司机存在: {}", phone);

        // 获取验证码
        R<Map<Object, Object>> r1 = serviceCodeClient.getVerificationCode(6);
        String code = (String) r1.getData().get("numberCode");
        log.info("验证码: {}", code);

        // 调用第三方发送验证码
        String key = RedisPrefixUtil.generateKey(phone, IdentityEnum.DRIVER.value());
        redisTemplate.opsForValue().set(key, code, 2, TimeUnit.MINUTES);

        return true;
    }

    public R<TokenResponse> checkCode(String phone, String code) {
        String key = RedisPrefixUtil.generateKey(phone, IdentityEnum.DRIVER.value());
        String redisCode = redisTemplate.opsForValue().get(key);
        if (StrUtil.isBlank(redisCode) || !code.trim().equals(redisCode)) {
            return R.fail("验证码不正确");
        }

        String newRefreshToken = JwtUtils.generateToken(phone, IdentityEnum.DRIVER.value(), TokenEnum.REFRESH_TOKEN.value());
        String accessToken = JwtUtils.generateToken(phone, IdentityEnum.DRIVER.value(), TokenEnum.ACCESS_TOKEN.value());

        String accessTokenKey = RedisPrefixUtil.generateTokenKey(phone, IdentityEnum.DRIVER.value(), TokenEnum.ACCESS_TOKEN.value());
        redisTemplate.opsForValue().set(accessTokenKey, accessToken, 30, TimeUnit.DAYS);
        String refreshTokenKey = RedisPrefixUtil.generateTokenKey(phone, IdentityEnum.DRIVER.value(), TokenEnum.REFRESH_TOKEN.value());
        redisTemplate.opsForValue().set(refreshTokenKey, newRefreshToken, 31, TimeUnit.DAYS);

        TokenResponse tokenResponse = new TokenResponse();
        tokenResponse.setRefreshToken(newRefreshToken).setAccessToken(accessToken);
        return R.ok(tokenResponse);
    }
}
